﻿

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;

namespace TImSdk
{
    [JsonConverter(typeof(ElemConverter))]
    public class Elem
    {
        private class ElemConverter : JsonCreationConverter<Elem>
        {
            protected override Elem Create(Type objectType,JObject jObject)
            {
                if(jObject.Property("elem_type") != null)
                {
                    if ((TIMElemType)Convert.ToInt32(jObject["elem_type"]) == TIMElemType.kTIMElem_Text)
                    {
                        return new TextElem();
                    }
                    else if ((TIMElemType)Convert.ToInt32(jObject["elem_type"]) == TIMElemType.kTIMElem_Image)
                    {
                        return new ImageElem();
                    }
                    else if ((TIMElemType)Convert.ToInt32(jObject["elem_type"]) == TIMElemType.kTIMElem_Sound)
                    {
                        return new SoundElem();
                    }
                    else if ((TIMElemType)Convert.ToInt32(jObject["elem_type"]) == TIMElemType.kTIMElem_Custom)
                    {
                        return new CustomElem();
                    }
                    else if ((TIMElemType)Convert.ToInt32(jObject["elem_type"]) == TIMElemType.kTIMElem_File)
                    {
                        return new FileElem();
                    }
                    else if ((TIMElemType)Convert.ToInt32(jObject["elem_type"]) == TIMElemType.kTIMElem_GroupTips)
                    {
                        return new GroupTipsElem();
                    }
                    else if ((TIMElemType)Convert.ToInt32(jObject["elem_type"]) == TIMElemType.kTIMElem_Face)
                    {
                        return new FaceElem();
                    }
                    else if ((TIMElemType)Convert.ToInt32(jObject["elem_type"]) == TIMElemType.kTIMElem_Location)
                    {
                        return new LocationElem();
                    }
                    else if ((TIMElemType)Convert.ToInt32(jObject["elem_type"]) == TIMElemType.kTIMElem_GroupReport)
                    {
                        return new GroupReportElem();
                    }
                    else if ((TIMElemType)Convert.ToInt32(jObject["elem_type"]) == TIMElemType.kTIMElem_Video)
                    {
                        return new VideoElem();
                    }
                    else if ((TIMElemType)Convert.ToInt32(jObject["elem_type"]) == TIMElemType.kTIMElem_FriendChange)
                    {
                        return new FriendChangeElem();
                    }
                    else if ((TIMElemType)Convert.ToInt32(jObject["elem_type"]) == TIMElemType.kTIMElem_ProfileChange)
                    {
                        return new ProfileChangeElem();
                    }
                    else
                    {
                        return new Elem();
                    }
                }
                else
                {
                    return new Elem();
                }
            }
        }

        [JsonProperty("elem_type")]
        public TIMElemType Elem_Type {get;set;} 
    }
}
